
	import classes.MDIPanel;
	import classes.MySQLService;
	import classes.events.MySqlErrorEvent;
	import classes.events.MySqlResultEvent;
	import classes.utils.CookieUtil;
	
	import components.CreateDBPanel;
	
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.managers.PopUpManager;
	
	[Bindable]
	[Embed(source='../assets/dbIcon.png')]
	public static var mxDbIcon:Class;
	
	[Bindable]
	[Embed(source='../assets/folderIconClosed.png')]
	public static var mxFolderIconCl:Class;
	
	[Bindable]
	[Embed(source='../assets/folderIconOpen.png')]
	public static var mxFolderIconOp:Class;
	
	[Bindable]
	[Embed(source='../assets/tableIcon.png')]
	public static var mxTableIcon:Class;
	
	[Bindable]
	[Embed(source='../assets/columnIcon.png')]
	public static var mxColumnIcon:Class;
	
	[Bindable]
	[Embed(source='../assets/indexIcon.png')]
	public static var mxIndexIcon:Class;
	
	[Bindable]
	[Embed(source='../assets/viewIcon.png')]
	public static var mxViewIcon:Class;
	
	[Bindable]
	[Embed(source='../assets/alertIcon.png')]
	public static var mxAlertIcon:Class;
	
		
	private function minimizedButtonClick(event:Event):void
	{
		var index:int = event.target.data;
		var panel:MDIPanel = mdiFrame.minimizedPanels.getItemAt(index) as MDIPanel;
		mdiFrame.restoreUp(panel, index);
	}
	
	
	private function disconnect_onClick( e:MouseEvent ):void
	{
		CookieUtil.deleteCookie( flashSQL.MD5_HASH );
		parentApplication.removeChild( this );
		parentApplication.initApp();
	}
	
	
	public function refreshDb_onCLick( e:MouseEvent ):void
	{
		mxDatabaseTree.enabled = false;
		mxDbHint.text = "Loading, plase wait...";
		
		var myService:MySQLService = new MySQLService( flashSQL.LANG_PREF ); 
		myService.getDatabaseList( dbListResult, dbListFault );
	}
	
	
	public function createDb_onClick( e:MouseEvent ):void
	{
		var createDB_Panel:CreateDBPanel = PopUpManager.createPopUp( this, CreateDBPanel, true ) as CreateDBPanel;
		PopUpManager.centerPopUp( createDB_Panel );	
	} 
	
	
	private function setIconsForItems( o:Object ):void
	{
		if( o.hasOwnProperty( 'children' ) )
		{
			setIconsForItems( o.children );
		}
		else if( o is Array )
		{
			for each( var obj:Object in o )
			{
				setIconsForItems( obj );
			}	
		}		
		
		if( o.icon == "mxFolderIcon" )
		{
			mxDatabaseTree.setItemIcon( o, mxFolderIconCl, mxFolderIconOp );
		}
		else if( o.icon == "mxColumnIcon" )
		{				
			mxDatabaseTree.setItemIcon( o, mxColumnIcon, mxColumnIcon );				 								
		}
		else if( o.icon == "mxIndexIcon" )
		{				
			mxDatabaseTree.setItemIcon( o, mxIndexIcon, mxIndexIcon );				 								
		}
		else if( o.icon == "mxTableIcon" )
		{
			mxDatabaseTree.setItemIcon( o, mxTableIcon, mxTableIcon );
		}
		else if( o.icon == "mxViewIcon" )
		{				
			mxDatabaseTree.setItemIcon( o, mxViewIcon, mxViewIcon );				 								
		}
		else if( o.icon == "mxDbIcon" )
		{				
			mxDatabaseTree.setItemIcon( o, mxDbIcon, mxDbIcon );				 								
		}
		
	}
	
	
	public function dbListResult( e:MySqlResultEvent ):void
	{
		var arrPtr:Array = e.result as Array;		
		
		setIconsForItems( arrPtr );
		
		mxDatabaseTree.enabled = true;
		
		mxDbHint.visible = false;
		
		mxDatabaseTree.dataProvider = arrPtr;
	}
	
	
	public function dbListFault( e:MySqlErrorEvent ):void
	{
		Alert.show( e.error.toString() );
	}